package com.huang.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.eduservice.client.OrderClient;
import com.huang.eduservice.entity.EduCourse;
import com.huang.eduservice.entity.chapter.ChapterVo;
import com.huang.eduservice.entity.frontVo.CourseFrontVo;
import com.huang.eduservice.entity.vo.CourseWebVo;
import com.huang.eduservice.service.EduChapterService;
import com.huang.eduservice.service.EduCourseService;
import com.hunag.utils.JwtUtils;
import com.hunag.utils.R;
import com.hunag.utils.orderVo.CourseWebVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @PostMapping("getPageCondition/{page}/{limit}")
    public R getPageCondition(@PathVariable long page,
                              @PathVariable long limit,
                              @RequestBody(required = false) CourseFrontVo courseFrontVo
                                ){
        Page<EduCourse> pageParam = new Page<>(page, limit);
       Map<String,Object> map=courseService.getCourseFrontList(pageParam,courseFrontVo);
        return R.ok().data(map);
    }

    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        CourseWebVo courseWebVo=courseService.getFrontCourseInfo(courseId);
        List<ChapterVo> chapterVideo = chapterService.getChapterVideo(courseId);
        if(!StringUtils.isEmpty(id)){
            boolean buy = orderClient.isBuy(id,courseId);
            return R.ok().data("chapterVideo",chapterVideo).data("courseWebVo",courseWebVo).data("isbuy",buy);
        }
        return R.ok().data("chapterVideo",chapterVideo).data("courseWebVo",courseWebVo);

    }
//    @GetMapping("getFrontCourseInfo/{courseId}")
//    public R getFrontCourseInfo(@PathVariable String courseId){
//
//        CourseWebVo courseWebVo=courseService.getFrontCourseInfo(courseId);
//        List<ChapterVo> chapterVideo = chapterService.getChapterVideo(courseId);
//        return R.ok().data("chapterVideo",chapterVideo).data("courseWebVo",courseWebVo);
//
//    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{courseId}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String courseId){
        CourseWebVo courseInfo = courseService.getFrontCourseInfo(courseId);
        CourseWebVoOrder voOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,voOrder);
        return  voOrder;
    }

}
